<?php
// 导入基础类
require_once(dirname(__FILE__) . '/base.class.php');
class Admin extends Base
{
    function __construct()
    {
        // 调用父类的构造函数
        parent::__construct();
    }

    function index()
    {
        $output = ['errcode' => 0, 'msg' => 'success'];
        echo json_encode($output);
    }

    // 获取管理员列表
    function lists(){
        $output=['errcode'=>0,'msg'=>'success'];
        if(isset($_GET['page'])){
            $page=$_GET['page']; 
        }
        if(isset($_GET['page'])){
            $pageNum=$_GET['page_num'];
        }
        if(!isset($page)){
            $page=1;
        }
        if(!isset($pageNum)){
            $pageNum=10;
        }
        $offset=($page-1)*$pageNum;
        $where='1=1';
        if(isset($_GET['nickname'])){
            $nickname=$_GET['nickname'];
            $where.=" and nickname like '%{$nickname}%'";
        }
        if(isset($_GET['username'])){
            $username=$_GET['username'];
            $where.=" and username like '%{$username}%'";
        }
        if(isset($_GET['tel'])){
            $tel=$_GET['tel'];
            $where.=" and tel like '%{$tel}%'";
        }
        if(isset($_GET['email'])){
            $email=$_GET['email'];
            $where.=" and email like '%{$email}%'";
        }
        $list=$this->db->query("select * from admins where ${where} limit {$offset},{$pageNum}");
        $total=$this->db->get_one("select count(id) as count from admins where ${where}")['count'];
        // 将头像路径处理成绝对路径
        foreach($list as $key=>$item){
            $list[$key]['avatar']=path($item['cover']);
        }
        $output['data']['list']=$list;
        $output['data']['total']=$total;
        echo json_encode($output);
    }

    // 密码重置
    function reset_pwd()
    {
        $output = ['errcode' => 0, 'msg' => 'success'];
        if ($_POST) {
            $password = md5($_POST['password']);
            $sql = "update admins set password='{$password}' where id={$this->admin['id']}";
            $res = $this->db->query($sql);
            if ($res) {
                $output['msg'] = '重置密码成功';
            } else {
                $output['errcode'] = 1;
                $output['msg'] = '重置密码失败';
            }
        } else {
            $output['errcode'] = 2;
            $output['msg'] = '请使用post方式提交';
        }
        echo json_encode($output);
    }


    // 更新用户信息
    function update()
    {
        $output = ['errcode' => 0, 'msg' => 'success'];
        if ($_POST) {
            $query_str = arr2str($_POST);
            $sql = "update admins set {$query_str} where id={$this->admin['id']}";
            $res = $this->db->query($sql);
            if ($res) {
                $output['msg'] = '信息更新成功';
            } else {
                $output['errcode'] = 3;
                $output['msg'] = '信息更新失败';
            }
        } else {
            $output['errcode'] = 2;
            $output['msg'] = '参数错误';
        }
        echo json_encode($output);
    }

    // 获取当前管理员的信息
    function login_info()
    {
        $output = ['errcode' => 0, 'msg' => 'success'];
        $sql="select * from admins where id={$this->admin['id']}";
        $login_info=$this->db->get_one($sql);
        // 将头像处理成绝对路径
        $login_info['avatar']=path($login_info['avatar']);
        $output['data'] = $login_info;
        echo  json_encode($output);
    }


    // 添加管理员账号
    function add_user()
    {
        $output = ['errcode' => 0, 'msg' => 'success'];
        if (!empty($_POST)) {
            $data = $_POST;
            $unique = $this->check_user_unique($data['username']);
            if (!$unique) {
                $output['errcode'] = 1;
                $output['msg'] = '用户名不可用';
            } else {
                $username = $data['username'];
                $password = md5($data['password']);
                $sql = "insert into admins set username='{$username}',password='{$password}'";
                $res = $this->db->query($sql);
                if ($res) {
                    $output['msg'] = '账号注册成功';
                } else {
                    $output['msg'] = '账号注册失败';
                }
            }
        } else {
            $output['errcode'] = 1;
            $output['msg'] = '请使用post方式提交';
        }
        echo json_encode($output);
    }

    // 重置其他管理员的信息
    function reset_admin()
    {
        $output = ['errcode' => 0, 'msg' => 'success'];
        if ($_POST) {
            $data=$_POST;
            if(isset($data['password'])){
                $data['password']=md5($data['password']);
            }
            if (isset($_GET['id'])) {
                $id = $_GET['id'];
                $query_str=arr2str($data);
                $sql = "update admins set {$query_str}  where id={$id} ";
                $res = $this->db->get_one($sql);
            } else {
                $output['errcode'] = 3;
                $output['msg'] = '参数错误';
            }
        } else {
            $output['errcode'] = 2;
            $output['msg'] = '请使用POST方式提交';
        }
    }


    // 获取其他管理员的信息
    function detail()
    {
        $output = ['errcode' => 0, 'msg' => 'success'];
        if (isset($_GET['id'])) {
            $id = $_GET['id'];
            $sql = "select * from admins where id={$id} limit 1";
            $data = $this->db->get_one($sql);
            // 将头像路径处理成绝对路径
            $data['avatar']=path($data['avatar']);
            $output['data'] = $data;
        } else {
            $output['errcode'] = '1';
            $output['msg'] = '参数错误';
        }
        echo json_encode($output);
    }


    // 校验用户名是否唯一
    private function check_user_unique($username)
    {
        $sql = "select id,username from admins where username='{$username}'";
        $res = $this->db->get_one($sql);
        if (empty($res)) {
            // 用户名可用
            return true;
        } else {
            // 用户名不可用
            return false;
        }
    }
}
